﻿Public Class frmSprintsInsert

    Dim strNameForm As String = "Insert Sprint"
    Public mId As Integer

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm
            cboBacklogs.Load("Select * From Backlogs", "Name", gConnectionStringAgenda.ToString)

            AddHandler cboBacklogs.OnInsert, AddressOf InterfaceManager.Backlogs_OnInsert
            AddHandler cboBacklogs.OnModify, AddressOf InterfaceManager.Backlogs_OnModify

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            If IsComplete() Then
                Dim oTSprints As New Data.TSprints
                oTSprints.mName = txtName.Text
                oTSprints.mStartDate = txtDate.DataMySql
                oTSprints.mBacklogsID = cboBacklogs.SelectedValue

                mId = oTSprints.Insert
                If mId > 0 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                Else
                    MsgBox("Impossible insert the sprint.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
                oTSprints = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtName.Text = "" Then
                MsgBox("Insert name.", vbExclamation, Me.Name)
                txtName.Focus()
                Return False
            End If
            If Not cboBacklogs.IsItemSelected Then
                MsgBox("Select backlog.", vbExclamation, Me.Name)
                cboBacklogs.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class